class PeopleController < ApplicationController
  before_filter :find_person,  :only => [ :show,
                                          :edit,
                                          :update,
                                          :destroy]

  helper_method :sort_column, :sort_direction
                                          
  # GET /people
  # GET /people.json
  def index
    @people = Person.search(params[:search]).order(sort_column + " " + sort_direction).page(params[:page])
    if params[:enabled_only] == "true"
      @people = @people.enabled_only
    end
    if params[:disabled_only] == "true"
      @people = @people.disabled_only
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @people }
    end
  end

  # GET /people/1
  # GET /people/1.json
  def show
    @person = Person.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @person }
    end
  end

  # GET /people/new
  # GET /people/new.json
  def new
    @person = Person.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @person }
    end
  end

  # GET /people/1/edit
  def edit
    @person = Person.find(params[:id])
  end

  # POST /people
  # POST /people.json
  def create
    @person = Person.new(params[:person])
    @person.church_id = 1 #TODO: this needs to be handled

    respond_to do |format|
      if @person.save
        format.html { redirect_to @person, notice: 'Person was successfully created.' }
        format.json { render json: @person, status: :created, location: @person }
      else
        format.html { render action: "new" }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /people/1
  # PUT /people/1.json
  def update
    @person = Person.find(params[:id])

    respond_to do |format|
      if @person.update_attributes(params[:person])
        format.html { redirect_to @person, notice: 'Person was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /people/1
  # DELETE /people/1.json
  def destroy
    @person = Person.find(params[:id])
    @person.enabled = false

    respond_to do |format|
      if @person.update_attributes(params[:person])
        format.html { redirect_to @person, notice: 'Person was successfully disabled.' }
        format.json { head :ok }
      else
        format.html { render action: "delete" }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end
  
  private
    def find_person
      @person = Person.find(params[:id])
      rescue ActiveRecord::RecordNotFound
      
      respond_to do |format|
        format.html { redirect_to people_url, alert: 'The person you were looking for could not be found.'  }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
    
    def sort_column
      Person.column_names.include?(params[:sort]) ? params[:sort] : "last_name"
    end
  
    def sort_direction
      %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
    end
end
